import zlib

lookup = []
code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
for str in code:
    lookup.append(str)


def tripletToBase64(num):
    return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]


def encodeChunk(uint8, start, end):
    tmp = ""
    output = []
    for i in range(start, end, 3):
        tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
        ac = tripletToBase64(tmp)
        print(ac)
        output.append(ac)
    return "".join(output)


def fromByteArray(uint8):
    tmp = ""
    l = len(uint8)
    extraBytes = l % 3  # if we have 1 byte left, pad 2 bytes
    output = ''
    parts = []
    maxChunkLength = 16383  # must be multiple of 3

    len2 = l - extraBytes
    # go through the array every three bytes, we'll deal with trailing stuff later
    for i in range(0, len2, maxChunkLength):
        temp = len2 if (i + maxChunkLength) > len2 else (i + maxChunkLength)
        parts.append(encodeChunk(uint8, i, temp))

    # pad the end with zeros, but make sure to not forget the extra bytes
    if extraBytes == 1:
        tmp = uint8[l - 1]
        output += lookup[tmp >> 2]
        output += lookup[(tmp << 4) & 0x3F]
        output += '=='
    elif extraBytes == 2:
        tmp = (uint8[l - 2] << 8) + (uint8[l - 1])
        output += lookup[tmp >> 10]
        output += lookup[(tmp >> 4) & 0x3F]
        output += lookup[(tmp << 2) & 0x3F]
        output += '='

    parts.append(output)

    return "".join(parts)


if __name__ == '__main__':
    with open(r"参数.json", "r", encoding="utf-8") as p:
        data = p.read()
        tmp = zlib.compress(bytes(data, "utf-8"))
        s = fromByteArray(tmp)
        print(s)
